package com.foreveredu.erlms.persist.hibernate;

import java.util.List;

import com.foreveredu.erlms.domain.Grade;
import com.foreveredu.erlms.domain.PageModel;
import com.foreveredu.erlms.persist.IGradeDao;

public class HibernateGradeDao extends HibernateAbstractDao<Grade, Integer> implements IGradeDao {

	private static final long serialVersionUID = 1L;

	public HibernateGradeDao() {
		super(Grade.class);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Grade findGradeByIDCardAndTicketNumber(String idCard, String ticketNumber, int examOrder) {
		Grade grade = null;
		List<Grade> gList = this.getHibernateTemplate().find("from Grade g where g.examinee.idCard = ? and g.examinee.ticketNumber = ? and examOrder = ?", new Object[]{idCard, ticketNumber, examOrder});
		if (gList != null && gList.size() > 0) {
			grade = gList.get(0);
		}
		return grade;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public PageModel findGradeList(int offset, int examOrder) {
		List<Grade> grades = this.getHibernateTemplate().find("from Grade g where g.examinee.examOrder = ? ",examOrder);
		PageModel pm = new PageModel();
		pm.setTotal(grades.size());
		if (offset + 10 > grades.size()) {
			pm.setDatas(grades.subList(offset, grades.size()));
			return pm;
		}
		pm.setDatas(grades.subList(offset, offset + 10));
		return pm;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Grade findGradeByExamineeId(int id) {
		Grade grade = null;
		List<Grade> gList = this.getHibernateTemplate().find("from Grade g where g.examinee.id = ? ",id);
		if (gList != null && gList.size() > 0) {
			grade = gList.get(0);
		}
		return grade;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public int findGradeByDeptIdAndZcj(int deptId,int examOrder) {
		int gradeOverNum = 0;
		List<Grade> gList = this.getHibernateTemplate().find("from Grade g where g.examinee.enroll.dept.id = ? and g.zcj = 0 and g.examinee.examOrder = ? " , new Object[] { deptId,examOrder});
		if (gList != null && gList.size() > 0) {
			gradeOverNum = gList.size();
		}
		return gradeOverNum;
	}

	@SuppressWarnings("unchecked")
	@Override
	public int findGradeByDeptId(int deptId, int examOrder) {
		int gradeAllNum = 0;
		List<Grade> gList = this.getHibernateTemplate().find("from Grade g where g.examinee.enroll.dept.id = ? and g.examinee.examOrder = ? " , new Object[] { deptId,examOrder});
		if (gList != null && gList.size() > 0) {
			gradeAllNum = gList.size();
		}
		return gradeAllNum;
	}
}
